package com.talkweb.osmharmony.tileprovider.modules;

import ohos.app.Context;
import ohos.bundle.IBundleManager;
import ohos.net.NetManager;
import ohos.wifi.WifiDevice;

/**
 * A straightforward network check implementation.
 *
 * @author Marc Kurtz
 */

public class NetworkAvailabliltyCheck implements INetworkAvailablityCheck {

    private final NetManager mNetworkManager;
    private final boolean mHasNetworkStatePermission;
    private final  Context mContext;

    public NetworkAvailabliltyCheck(final Context aContext) {
        mContext = aContext;

        mNetworkManager = NetManager.getInstance(aContext);

        mHasNetworkStatePermission = IBundleManager.PERMISSION_GRANTED
                == aContext.verifyCallingOrSelfPermission("ohos.permission.GET_NETWORK_INFO");

    }

    @Override
    public boolean getNetworkAvailable() {
        if (!mHasNetworkStatePermission) {
            // if we're unable to check network state, assume we have a network
            return true;
        }

        //TODO 非wifi网络，比如以太网； 鸿蒙暂时没有提供相关以太网络状态获取API接口

        return true;
    }

    @Override
    public boolean getWiFiNetworkAvailable() {
        if (!mHasNetworkStatePermission) {
            // if we're unable to check network state, assume we have a network
            return true;
        }

        WifiDevice wifiDevice = WifiDevice.getInstance(mContext);
        return wifiDevice != null && wifiDevice.isWifiActive() && wifiDevice.isConnected();
    }

    @Override
    public boolean getCellularDataNetworkAvailable() {
        if (!mHasNetworkStatePermission) {
            // if we're unable to check network state, assume we have a network
            return true;
        }

        //TODO 移动蜂窝网络；  鸿蒙暂时没有提供相关蜂窝网络状态获取API接口

        return true;
    }

    @Deprecated
    @Override
    public boolean getRouteToPathExists(final int hostAddress) {
        // TODO check for CHANGE_NETWORK_STATE permission
        //return mConnectionManager.requestRouteToHost(ConnectivityManager.TYPE_WIFI, hostAddress)
        //	|| mConnectionManager.requestRouteToHost(ConnectivityManager.TYPE_MOBILE, hostAddress);
        return true;
    }

}
